package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        Integer pageSize = deviceDataPageReqDto.getPageSize();
        Integer pageNum = deviceDataPageReqDto.getPageNum();
        PageHelper.startPage(pageNum, pageSize);
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isAllEmpty(startTime)?null: LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isAllEmpty(endTime)?null: LocalDateTimeUtil.of(endTime));
        Page<DeviceDataVo>page =deviceDataMapper.selectByPage(deviceDataPageReqDto);
        return PageResponse.of(page, DeviceDataVo.class);

    }

    @Override
    public Object queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime starDateTime = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDateTime = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.dayInstance(starDateTime);
        List<DeviceDataGraphVo> deviceDataGraphVosDbs=deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, starDateTime, endDateTime);
        deviceDataGraphVosDbs.forEach(deviceDataGraphVoDb -> {
            deviceDataGraphVos.forEach(deviceDataGraphVo -> {
                if (deviceDataGraphVo.getDateTime().equals(deviceDataGraphVoDb.getDateTime())) {
                    deviceDataGraphVo.setDataValue(deviceDataGraphVoDb.getDataValue());
                }
            });
        });
    return deviceDataGraphVos;}

    @Override
    public ResponseResult<List<DeviceDataGraphVo>> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startDateTime = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDateTime = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.weekInstance(startDateTime);
        List<DeviceDataGraphVo> deviceDataGraphVosDbs=deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, startDateTime, endDateTime);
        deviceDataGraphVosDbs.forEach(deviceDataGraphVoDb -> {
            deviceDataGraphVos.forEach(deviceDataGraphVo -> {
                if (deviceDataGraphVo.getDateTime().equals(deviceDataGraphVoDb.getDateTime())){
                    deviceDataGraphVo.setDataValue(deviceDataGraphVoDb.getDataValue());
                }
            });
        });
        return ResponseResult.success(deviceDataGraphVos);
    }
}
